/**
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 * @data:2017年10月17日
 * @author:MaShibo
 */
package com.wttech.dzzf.process.service.user;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.dao.VehicleValidInfoDao;
import com.wttech.dzzf.process.model.user.VehicleValid;

import cn.com.taiji.sdk.model.comm.protocol.valid.VehiclePlateCheckValidateRequest;

/**
 * 车牌验证信息创建服务
 * 
 * @date 2017年10月17日
 * @author MaShibo
 */
@Service
public class VehicleValidInfosService {
    
    
    private static final Logger LOGGER = LoggerFactory.getLogger(VehicleValidInfosService.class);
    
    private static final int DEFAULT_PROCESS_NUM = 10000;
    
    @Resource
    private VehicleValidInfoDao vehicleValidInfoDao;
    
    @Resource
    private TransferUserInfoDao transferUserInfoDao;
    
    @Resource
    private ThreadPoolTaskExecutor executor;
    
    public void createVehicleValidInfos() {
        Integer total = vehicleValidInfoDao.queryVehicleInfosNum();
        LOGGER.info("共{}条车辆信息记录", total);
        int count = (int) Math.ceil((double) total / DEFAULT_PROCESS_NUM);
        LOGGER.info("共需要执行{}次", count);
        List<Future<Void>> futures = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            futures.add(executor.submit(new SubProcessThread(i + 1)));
        }
        for (Future<Void> future : futures) {
            try {
                future.get();
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage(), e);
                Thread.currentThread().interrupt();
            } catch (ExecutionException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        executor.shutdown();
    }
    
    public class SubProcessThread implements Callable<Void> {
        
        
        private int count;
        
        public SubProcessThread(int count) {
            super();
            this.count = count;
        }
        
        @Override
        public Void call() throws Exception {
            LOGGER.info("第{}次开始执行...", count);
            int startRow = (count - 1) * DEFAULT_PROCESS_NUM + 1;
            int endRow = count * DEFAULT_PROCESS_NUM;
            List<VehiclePlateCheckValidateRequest> vehicleValids = vehicleValidInfoDao
                    .queryVehicleValidReqInfos(startRow, endRow);
            for (VehiclePlateCheckValidateRequest vehicleValid : vehicleValids) {
                createVehicleValidInfo(vehicleValid);
            }
            LOGGER.info("第{}次结束执行", count);
            return null;
        }
        
        private void createVehicleValidInfo(VehiclePlateCheckValidateRequest vehicleValid) {
            VehicleValid vv = new VehicleValid();
            String[] splits = StringUtils.split(vehicleValid.getVehiclePlate(), '_');
            String vehPlate = splits[0];
            Integer vehColor = Integer.valueOf(splits[1]);
            vv.setVehiclePlate(vehPlate);
            vv.setVehicleColor(vehColor);
            vv.setVehicleType(vehicleValid.getVehicleType());
            try {
                transferUserInfoDao.createTestObjcet(vv);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
                LOGGER.info("错误的验证信息{}", vv.toString());
            }
        }
        
    }
    
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        VehicleValidInfosService service = context.getBean(VehicleValidInfosService.class);
        service.createVehicleValidInfos();
    }
}
